package com.frank.service.impl;

import com.alibaba.fastjson.JSON;
import com.frank.dao.JobRecordMapper;
import com.frank.dao.RecommendMapper;
import com.frank.dto.CommonListDto;
import com.frank.dto.JobRecordDto;
import com.frank.dto.QueryPageDto;
import com.frank.dto.RecruitPoolDto;
import com.frank.feign.ResumeFeign;
import com.frank.model.JobRecord;
import com.frank.model.JobRecordCriteria;
import com.frank.model.Resume;
import com.frank.service.ResumePoolService;
import com.frank.utils.RecruitProcessEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ResumePoolServiceImpl implements ResumePoolService {

    @Resource
    private JobRecordMapper jobRecordMapper;
    @Resource
    private RecommendMapper recommendMapper;

    @Resource
    private ResumeFeign resumeFeign;


    @Override
    public CommonListDto getResumeList(String recruitType, QueryPageDto queryPageDto) {

        int count = jobRecordMapper.selectResumeCountByRecruitType(recruitType);

        HashMap<String,String> hashMap = queryPageDto.getMap();
        hashMap.put("recruitType",recruitType);

        List<Map> mapList = jobRecordMapper.selectResumeByRecruitType(hashMap,queryPageDto.getRowBounds());
        List<RecruitPoolDto> recruitPoolDtoList = new ArrayList<>();
        for (Map map : mapList){
            RecruitPoolDto recruitPoolDto = new RecruitPoolDto();
            recruitPoolDto.setDegree((String)map.get("degree"));
            if (map.get("recommend_id") != null){
                recruitPoolDto.setIs_recommend("是");
            }else {
                recruitPoolDto.setIs_recommend("否");
            }
            recruitPoolDto.setJob((String)map.get("job_name"));
            recruitPoolDto.setName((String)map.get("real_name"));
            recruitPoolDto.setProcess_state(RecruitProcessEnum.getName((Integer)map.get("process_state")));
            recruitPoolDto.setSchool((String)map.get("college"));
            recruitPoolDto.setSex((String)map.get("sex"));
            recruitPoolDto.setRemark((String)map.get("remark"));
            recruitPoolDto.setUserId((Integer)map.get("user_id"));
            recruitPoolDto.setResumeId((Integer)map.get("resume_id"));
            recruitPoolDto.setJobRecordId((Integer)map.get("id"));

            recruitPoolDtoList.add(recruitPoolDto);
        }
        return new CommonListDto<>(recruitPoolDtoList,count);
    }

    @Override
    public boolean updateJobRecord(JobRecord jobRecord) {
        return jobRecordMapper.updateByPrimaryKeySelective(jobRecord) == 1;
    }

    @Override
    public boolean deleteJobRecord(Integer jobRecordId) {
        return jobRecordMapper.deleteByPrimaryKey(jobRecordId) == 1;
    }

    @Override
    public boolean submitJobWish(JobRecord jobRecord) {
        Integer recommend_id = recommendMapper.selectIdByUserId(jobRecord.getUserId());
        if (recommend_id != null) {
            jobRecord.setRecommendId(recommend_id);
        }

        Resume resume = resumeFeign.getByUserId(jobRecord.getUserId()).getData();
        jobRecord.setResumeId(resume.getId());

        JobRecordCriteria jobRecordCriteria = new JobRecordCriteria();
//        jobRecordCriteria.createCriteria().andUserIdEqualTo(jobRecord.getUserId()).andJobIdEqualTo(jobRecord.getJobId()).andIsSuccessIsNull();
        jobRecordCriteria.createCriteria().andUserIdEqualTo(jobRecord.getUserId()).andIsSuccessIsNull();
        List<JobRecord> jobRecordList = jobRecordMapper.selectByExample(jobRecordCriteria);
        if (jobRecordList.isEmpty()){
            return jobRecordMapper.insertSelective(jobRecord) == 1;
        }else {
            return false;
        }
    }

    @Override
    public List<JobRecordDto> getJobRecordByUserId(Integer userId) {
        List<Map> mapList = jobRecordMapper.selectJobRecordListByUserId(userId);

        List<JobRecordDto> jobRecordDtoList = new ArrayList<>();

        for (Map map: mapList){
            JobRecordDto jobRecordDto = new JobRecordDto();
            jobRecordDto.setIs_recommend(map.get("recommend_id") != null ? "是":"否");
            jobRecordDto.setJob_name((String)map.get("job_name"));
            jobRecordDto.setProcess_state(map.get("process_state")+"");
            jobRecordDto.setRecruit_type((String)map.get("recruit_type"));

            Date createTime = (Date)map.get("create_time");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            jobRecordDto.setDeliver_time(simpleDateFormat.format(createTime));

            if (map.get("is_success") == null){
                jobRecordDto.setRecruit_state("正在进行");
                jobRecordDto.setIs_success("process");
            }else {
                jobRecordDto.setRecruit_state((Integer)map.get("is_success") == 1 ? "已完成" : "未通过");
                jobRecordDto.setIs_success((Integer)map.get("is_success") == 1 ? "finish" : "error");
            }

            jobRecordDtoList.add(jobRecordDto);
        }

        return jobRecordDtoList;
    }
}
